<?php
/*
 *
 *
*/
namespace app\admin\controller;

use think\Config;
use think\Db;
use think\Request;
use think\Cache;
use think\Loader;
use think\Session;
use think\Controller;
class Muban extends Common
{
    public function zaixian()
    {
        $or = input('or');
        $this->assign('or', $or);
        return view();
    }
    //下载pc
    public function xiazai()
    {
        $id = $_GET['id'];
        $info = $_GET['info'];
        $name = file_get_contents('' . jiami('71c9nry2RClA9yRswQwkzRjFUzqQjqc+yjDJBL53fp1d6UdJXQgOcyVg8+5DAENdLxZ9N7EelTWW1llbo95K8BFomL9geAa6OPzM4mA', 'DECODE', 'api', 0) . '' . www() . '&tid=' . $info . '');
        if ($info == $name) {
            $xiazai = file_get_contents('' . jiami('e5d27rtP4m8YXAEtMmD77PjflnhGGrRIVEzv4wbYAsp80d/ypcf14uzzz9w2P04/FIXhrykd4d53Q7jyYYPnx6QRt4LvsrehkffGfkc', 'DECODE', 'api', 0) . '' . www() . '&tid=' . $info . '');
            $url = $xiazai . urlencode(iconv("GB2312", "UTF-8", ""));
            $save_dir = 'app/extra/';
            $filename = 'muban.zip';
            $res = getFile($url, $save_dir, $filename, 0);
            return json(array('code' => 200, 'msg' => '下载成功'));
        } else {
            return json(array('code' => 0, 'msg' => '正在跳转到购买页面', 'path' => pa('EDu2ZX%3DfZHzUgEXdsIK%3DfXO1V9b4pChJbQvkc8r72') . $id . '.html'));
        }
    }
    //下载wap
    public function xiazaiw()
    {
        $id = $_GET['id'];
        $info = $_GET['info'];
        $name = file_get_contents('' . jiami('08cb2k9SAsHI40cdz5sx0eSVpX30sDZrEyYFLyo3UPOkwRJSLsjjl5F+efbRtNWk1qU0g2nRNuoIhlTcgla5R+2dfCaxajFNFJVF9egf', 'DECODE', 'api', 0) . '' . www() . '&tid=' . $info . '');
        if ($info == $name) {
            $xiazai = file_get_contents('' . jiami('1db5KYzs2On3iglBQNe3+trhBCfGSCHfE6J8Jodo1P+BYHtPWrzjJVPu0v6cxlVhqMF5NAFOp7VNQTJ//lkY0HoBA8UUiAcReBtVVQPe', 'DECODE', 'api', 0) . '' . www() . '&tid=' . $info . '');
            $url = $xiazai . urlencode(iconv("GB2312", "UTF-8", ""));
            $save_dir = 'app/extra/';
            $filename = 'muban.zip';
            $res = getFile($url, $save_dir, $filename, 0);
            return json(array('code' => 200, 'msg' => '下载成功'));
        } else {
            return json(array('code' => 0, 'msg' => '正在跳转到购买页面', 'path' => pa('EDu2ZX%3DfZHzUgEXdsIK%3DfXO1V9b4pChJbQvkc8r72') . $id . '.html'));
        }
    }
    //解压
    public function ad()
    {
        $zip = new \ZipArchive();
        if ($zip->open('app/extra/muban.zip') === TRUE) {
            $zip->extractTo('app/template/');
            $zip->close();
            unlink('app/extra/muban.zip');
            header("content-type:text/html;charset=utf-8");
            header('location:/admin.php/muban/index.html');
        }
    }
    //删除pc
    public function un()
    {
        $info = preg_replace("/\./","",$_GET['info']);
        $res = deleteun(ROOT_PATH . 'app/template/pc' . DS . $info);
        if ($res) {
            return json(array('code' => 200, 'msg' => '删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '删除失败'));
        }
    }
    //删除wap
    public function unw()
    {
        $info = preg_replace("/\./","",$_GET['info']);
        $res = deleteun(ROOT_PATH . 'app/template/wap' . DS . $info);
        if ($res) {
            return json(array('code' => 200, 'msg' => '删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '删除失败'));
        }
    }
    //开关手机模板
    public function kg()
    {
        $path = 'app/extra/web.php';
        $file = (include $path);
        $config = array('WAPOPEN' => input('id'));
        $config = preg_replace("/[?><?'=]/", '', $config);
        $res = array_merge($file, $config);
        $str = '<?php return [';
        foreach ($res as $key => $value) {
            $str .= '\'' . $key . '\'' . '=>' . '\'' . $value . '\'' . ',';
        }
        $str .= ']; ';
        if (file_put_contents($path, $str)) {
            return json(array('code' => 200, 'msg' => '操作成功'));
        } else {
            return json(array('code' => 0, 'msg' => '操作失败'));
        }
    }
    public function add()
    {
        $u = jiami('71c9nry2RClA9yRswQwkzRjFUzqQjqc+yjDJBL53fp1d6UdJXQgOcyVg8+5DAENdLxZ9N7EelTWW1llbo95K8BFomL9geAa6OPzM4mA', 'DECODE', 'api', 0);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $u);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_exec($ch);
        $curl_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        if ($curl_code == 200 || $curl_code == 302 || $curl_code == 301) {
            $name = file_get_contents('' . $u . '' . www() . '&tid=' . input('WEB') . '');
        } else {
            $name = input('WEB');
        }
        if (input('WEB')=='default'||input('WEB')=='default_pc') {
        	$name = input('WEB');
        }
        if (input('WEB') == $name||$_SERVER['HTTP_HOST']==pa('JGVGwNPusGE2q')) {
            $path = 'app/extra/web.php';
            $file = (include $path);
            $config = array('WEB' => input('WEB'));
            $res = array_merge($file, $config);
            $str = '<?php return [';
            foreach ($res as $key => $value) {
                $str .= '\'' . $key . '\'' . '=>' . '\'' . $value . '\'' . ',';
            }
            $str .= ']; ';
            if (file_put_contents($path, $str)) {
                Db::name('set')->where(array('set' => 'default'))->setField('name', input('WEB'));
                return json(array('code' => 200, 'msg' => '启动成功'));
            } else {
                return json(array('code' => 0, 'msg' => '启动失败'));
            }
        } else {
            return json(array('code' => 0, 'msg' => jiami('8c9ftcADK2Zs9H414sKPs5f57JVJQww7LTI8H2gLlWHEYnhX6lBZqLsdlzU', 'DECODE', '115', 0)));
        }
    }
    public function addw()
    {
        $u = jiami('08cb2k9SAsHI40cdz5sx0eSVpX30sDZrEyYFLyo3UPOkwRJSLsjjl5F+efbRtNWk1qU0g2nRNuoIhlTcgla5R+2dfCaxajFNFJVF9egf', 'DECODE', 'api', 0);
        $us = jiami('71c9nry2RClA9yRswQwkzRjFUzqQjqc+yjDJBL53fp1d6UdJXQgOcyVg8+5DAENdLxZ9N7EelTWW1llbo95K8BFomL9geAa6OPzM4mA', 'DECODE', 'api', 0);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $u);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_exec($ch);
        $curl_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        if ($curl_code == 200 || $curl_code == 302 || $curl_code == 301) {
            $name = file_get_contents('' . $u . '' . www() . '&tid=' . input('WAP') . '');
            $names = file_get_contents('' . $us . '' . www() . '&tid=' . input('WAP') . '');
        } else {
            $name = input('WAP');
            $names = input('WAP');
        }
        if (input('WAP')=='default_wap') {
        	$name = input('WAP');
        	$names = input('WAP');
        }
        if (input('WAP') == $name||$_SERVER['HTTP_HOST']==pa('JGVGwNPusGE2q')||input('WAP')==$names) {
            $path = 'app/extra/web.php';
            $file = (include $path);
            $config = array('WAP' => input('WAP'));
            $config = preg_replace("/[?><?'=]/", '', $config);
            $res = array_merge($file, $config);
            $str = '<?php return [';
            foreach ($res as $key => $value) {
                $str .= '\'' . $key . '\'' . '=>' . '\'' . $value . '\'' . ',';
            }
            $str .= ']; ';
            if (file_put_contents($path, $str)) {
                Db::name('set')->where(array('set' => 'defaultwap'))->setField('name', input('WAP'));
                return json(array('code' => 200, 'msg' => '启动成功'));
            } else {
                return json(array('code' => 0, 'msg' => '启动失败'));
            }
        } else {
            return json(array('code' => 0, 'msg' => jiami('8c9ftcADK2Zs9H414sKPs5f57JVJQww7LTI8H2gLlWHEYnhX6lBZqLsdlzU', 'DECODE', '115', 0)));
        }
    }
    //pc模板获取
    public function cms_pc($name)
    {
        $url = DS . 'app/template/pc/' . DS;
        $info = array();
        if (is_dir(ROOT_PATH . $url . $name . DS)) {
            $info = (include ROOT_PATH . $url . $name . DS . 'Cms.php');
            $info['name'] = $name;
            return $info;
        }
    }
    public function cms_wap($name)
    {
        $url = DS . 'app/template/wap/' . DS;
        $info = array();
        if (is_dir(ROOT_PATH . $url . $name . DS)) {
            $info = (include ROOT_PATH . $url . $name . DS . 'Cms.php');
            $info['name'] = $name;
            return $info;
        }
    }
    //首页
    public function index()
    {
        $muban = array();
        $mubans = get_115cms(ROOT_PATH . 'app/template/pc/' . DS);
        foreach ($mubans as $name) {
            $muban[] = $this->cms_pc($name);
        }
        $this->assign('muban', $muban);
        $mubanw = array();
        $mubansw = get_115cms(ROOT_PATH . 'app/template/wap/' . DS);
        foreach ($mubansw as $name) {
            $mubanw[] = $this->cms_wap($name);
        }
        $this->assign('mubanw', $mubanw);
        return view();
    }
}